"""
岗位管理模型
"""
from django.db import models
from django.conf import settings
from students.models import Student


class Company(models.Model):
    """公司信息"""
    
    COMPANY_TYPES = [
        ('startup', '初创公司'),
        ('sme', '中小企业'),
        ('large', '大型企业'),
        ('multinational', '跨国公司'),
        ('government', '政府机构'),
        ('ngo', '非营利组织'),
    ]
    
    COMPANY_SIZES = [
        ('1-10', '1-10人'),
        ('11-50', '11-50人'),
        ('51-200', '51-200人'),
        ('201-500', '201-500人'),
        ('501-1000', '501-1000人'),
        ('1000+', '1000人以上'),
    ]
    
    name = models.CharField(max_length=200, unique=True, verbose_name='公司名称')
    company_type = models.CharField(max_length=20, choices=COMPANY_TYPES, verbose_name='公司类型')
    company_size = models.CharField(max_length=20, choices=COMPANY_SIZES, verbose_name='公司规模')
    industry = models.CharField(max_length=100, verbose_name='所属行业')
    description = models.TextField(blank=True, null=True, verbose_name='公司简介')
    website = models.URLField(blank=True, null=True, verbose_name='公司网站')
    logo = models.ImageField(upload_to='company_logos/', blank=True, null=True, verbose_name='公司Logo')
    
    # 联系信息
    contact_person = models.CharField(max_length=100, blank=True, null=True, verbose_name='联系人')
    contact_phone = models.CharField(max_length=20, blank=True, null=True, verbose_name='联系电话')
    contact_email = models.EmailField(blank=True, null=True, verbose_name='联系邮箱')
    address = models.TextField(blank=True, null=True, verbose_name='公司地址')
    
    is_active = models.BooleanField(default=True, verbose_name='是否启用')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'jobs_company'
        verbose_name = '公司'
        verbose_name_plural = '公司'
        ordering = ['name']
        
    def __str__(self):
        return self.name


class Job(models.Model):
    """岗位信息"""
    
    JOB_TYPES = [
        ('full_time', '全职'),
        ('part_time', '兼职'),
        ('internship', '实习'),
        ('contract', '合同工'),
        ('remote', '远程工作'),
    ]

    WORK_MODES = [
        ('online', '线上'),
        ('offline', '线下'),
        ('hybrid', '混合'),
    ]
    
    STATUS_CHOICES = [
        ('draft', '草稿'),
        ('published', '已发布'),
        ('closed', '已关闭'),
        ('expired', '已过期'),
    ]
    
    company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name='所属公司')
    title = models.CharField(max_length=200, verbose_name='职位名称')
    job_type = models.CharField(max_length=20, choices=JOB_TYPES, default='full_time', verbose_name='工作性质')
    work_mode = models.CharField(max_length=20, choices=WORK_MODES, default='offline', verbose_name='工作方式')
    department = models.CharField(max_length=100, blank=True, null=True, verbose_name='所属部门')
    
    # 薪资信息
    salary_min = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, verbose_name='最低薪资')
    salary_max = models.DecimalField(max_digits=10, decimal_places=2, blank=True, null=True, verbose_name='最高薪资')
    salary_negotiable = models.BooleanField(default=False, verbose_name='薪资面议')
    
    # 工作地点
    work_location = models.CharField(max_length=200, verbose_name='工作地点')
    remote_allowed = models.BooleanField(default=False, verbose_name='允许远程')
    
    # 职位要求
    description = models.TextField(verbose_name='职位描述')
    requirements = models.TextField(verbose_name='任职要求')
    responsibilities = models.TextField(verbose_name='工作职责')
    benefits = models.TextField(blank=True, null=True, verbose_name='福利待遇')
    
    # 技能要求
    required_skills = models.TextField(blank=True, null=True, verbose_name='必需技能')
    preferred_skills = models.TextField(blank=True, null=True, verbose_name='优先技能')
    
    # 学历要求
    education_requirement = models.CharField(max_length=50, blank=True, null=True, verbose_name='学历要求')
    experience_requirement = models.CharField(max_length=50, blank=True, null=True, verbose_name='经验要求')
    
    # 招聘信息
    positions_available = models.IntegerField(default=1, verbose_name='招聘人数')
    application_deadline = models.DateTimeField(blank=True, null=True, verbose_name='申请截止时间')

    # 联系方式
    contact_email = models.EmailField(verbose_name='简历投递邮箱')
    contact_phone = models.CharField(max_length=20, blank=True, null=True, verbose_name='联系电话')

    # 有效期设置
    is_long_term = models.BooleanField(default=False, verbose_name='长期有效')
    valid_from = models.DateTimeField(blank=True, null=True, verbose_name='有效期开始')
    valid_until = models.DateTimeField(blank=True, null=True, verbose_name='有效期结束')
    
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft', verbose_name='状态')
    is_featured = models.BooleanField(default=False, verbose_name='是否推荐')
    
    # 发布信息
    published_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        limit_choices_to={'role__in': ['admin', 'teacher']},
        verbose_name='发布人'
    )
    published_at = models.DateTimeField(blank=True, null=True, verbose_name='发布时间')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'jobs_job'
        verbose_name = '岗位'
        verbose_name_plural = '岗位'
        ordering = ['-created_at']
        
    def __str__(self):
        return f"{self.company.name} - {self.title}"
    
    @property
    def salary_range(self):
        """薪资范围"""
        if self.salary_negotiable:
            return "面议"
        elif self.salary_min and self.salary_max:
            return f"{self.salary_min}-{self.salary_max}"
        elif self.salary_min:
            return f"{self.salary_min}+"
        return "未提供"


class JobApplication(models.Model):
    """岗位申请"""
    
    STATUS_CHOICES = [
        ('pending', '待审核'),
        ('reviewing', '审核中'),
        ('approved', '已通过'),
        ('interview_scheduled', '面试安排'),
        ('interviewed', '已面试'),
        ('accepted', '已录用'),
        ('rejected', '已拒绝'),
        ('withdrawn', '已撤回'),
    ]
    
    job = models.ForeignKey(Job, on_delete=models.CASCADE, verbose_name='申请岗位')
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name='申请学生')
    
    # 申请信息
    cover_letter = models.TextField(blank=True, null=True, verbose_name='求职信')
    resume_file = models.FileField(upload_to='job_applications/', blank=True, null=True, verbose_name='简历文件')
    
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending', verbose_name='申请状态')
    
    # 审核信息
    reviewed_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        limit_choices_to={'role__in': ['admin', 'teacher']},
        verbose_name='审核人'
    )
    reviewed_at = models.DateTimeField(blank=True, null=True, verbose_name='审核时间')
    review_comments = models.TextField(blank=True, null=True, verbose_name='审核意见')
    
    # 面试信息
    interview_scheduled_at = models.DateTimeField(blank=True, null=True, verbose_name='面试时间')
    interview_location = models.CharField(max_length=200, blank=True, null=True, verbose_name='面试地点')
    interview_notes = models.TextField(blank=True, null=True, verbose_name='面试备注')
    
    applied_at = models.DateTimeField(auto_now_add=True, verbose_name='申请时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'jobs_application'
        verbose_name = '岗位申请'
        verbose_name_plural = '岗位申请'
        unique_together = ['job', 'student']
        ordering = ['-applied_at']
        
    def __str__(self):
        return f"{self.student.name} - {self.job.title}"


class JobRecommendation(models.Model):
    """岗位推荐"""
    
    student = models.ForeignKey(Student, on_delete=models.CASCADE, verbose_name='推荐学生')
    job = models.ForeignKey(Job, on_delete=models.CASCADE, verbose_name='推荐岗位')
    recommended_by = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        on_delete=models.CASCADE,
        limit_choices_to={'role__in': ['admin', 'teacher']},
        verbose_name='推荐人'
    )
    
    match_score = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='匹配度')
    reason = models.TextField(blank=True, null=True, verbose_name='推荐理由')
    
    is_viewed = models.BooleanField(default=False, verbose_name='是否已查看')
    is_applied = models.BooleanField(default=False, verbose_name='是否已申请')
    
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='推荐时间')
    
    class Meta:
        db_table = 'jobs_recommendation'
        verbose_name = '岗位推荐'
        verbose_name_plural = '岗位推荐'
        unique_together = ['student', 'job']
        ordering = ['-match_score', '-created_at']
        
    def __str__(self):
        return f"推荐 {self.job.title} 给 {self.student.name}"
